home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / base64.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  11KB  |  361 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''RFC 3548: Base16, Base32, Base64 Data Encodings'''
  5. import re
  6. import struct
  7. import binascii
  8. __all__ = [
  9.     'encode',
  10.     'decode',
  11.     'encodestring',
  12.     'decodestring',
  13.     'b64encode',
  14.     'b64decode',
  15.     'b32encode',
  16.     'b32decode',
  17.     'b16encode',
  18.     'b16decode',
  19.     'standard_b64encode',
  20.     'standard_b64decode',
  21.     'urlsafe_b64encode',
  22.     'urlsafe_b64decode']
  23. _translation = [ chr(_x) for _x in range(256) ]
  24. EMPTYSTRING = ''
  25.  
  26. def _translate(s, altchars):
  27.     translation = _translation[:]
  28.     for k, v in altchars.items():
  29.         translation[ord(k)] = v
  30.     
  31.     return s.translate(''.join(translation))
  32.  
  33.  
  34. def b64encode(s, altchars = None):
  35.     """Encode a string using Base64.
  36.  
  37.     s is the string to encode.  Optional altchars must be a string of at least
  38.     length 2 (additional characters are ignored) which specifies an
  39.     alternative alphabet for the '+' and '/' characters.  This allows an
  40.     application to e.g. generate url or filesystem safe Base64 strings.
  41.  
  42.     The encoded string is returned.
  43.     """
  44.     encoded = binascii.b2a_base64(s)[:-1]
  45.     if altchars is not None:
  46.         return _translate(encoded, {
  47.             '+': altchars[0],
  48.             '/': altchars[1] })
  49.  
  50.  
  51. def b64decode(s, altchars = None):
  52.     """Decode a Base64 encoded string.
  53.  
  54.     s is the string to decode.  Optional altchars must be a string of at least
  55.     length 2 (additional characters are ignored) which specifies the
  56.     alternative alphabet used instead of the '+' and '/' characters.
  57.  
  58.     The decoded string is returned.  A TypeError is raised if s were
  59.     incorrectly padded or if there are non-alphabet characters present in the
  60.     string.
  61.     """
  62.     if altchars is not None:
  63.         s = _translate(s, {
  64.             altchars[0]: '+',
  65.             altchars[1]: '/' })
  66.     
  67.     try:
  68.         return binascii.a2b_base64(s)
  69.     except binascii.Error:
  70.         msg = None
  71.         raise TypeError(msg)
  72.  
  73.  
  74.  
  75. def standard_b64encode(s):
  76.     '''Encode a string using the standard Base64 alphabet.
  77.  
  78.     s is the string to encode.  The encoded string is returned.
  79.     '''
  80.     return b64encode(s)
  81.  
  82.  
  83. def standard_b64decode(s):
  84.     '''Decode a string encoded with the standard Base64 alphabet.
  85.  
  86.     s is the string to decode.  The decoded string is returned.  A TypeError
  87.     is raised if the string is incorrectly padded or if there are non-alphabet
  88.     characters present in the string.
  89.     '''
  90.     return b64decode(s)
  91.  
  92.  
  93. def urlsafe_b64encode(s):
  94.     """Encode a string using a url-safe Base64 alphabet.
  95.  
  96.     s is the string to encode.  The encoded string is returned.  The alphabet
  97.     uses '-' instead of '+' and '_' instead of '/'.
  98.     """
  99.     return b64encode(s, '-_')
  100.  
  101.  
  102. def urlsafe_b64decode(s):
  103.     """Decode a string encoded with the standard Base64 alphabet.
  104.  
  105.     s is the string to decode.  The decoded string is returned.  A TypeError
  106.     is raised if the string is incorrectly padded or if there are non-alphabet
  107.     characters present in the string.
  108.  
  109.     The alphabet uses '-' instead of '+' and '_' instead of '/'.
  110.     """
  111.     return b64decode(s, '-_')
  112.  
  113. _b32alphabet = {
  114.     0: 'A',
  115.     9: 'J',
  116.     18: 'S',
  117.     27: '3',
  118.     1: 'B',
  119.     10: 'K',
  120.     19: 'T',
  121.     28: '4',
  122.     2: 'C',
  123.     11: 'L',
  124.     20: 'U',
  125.     29: '5',
  126.     3: 'D',
  127.     12: 'M',
  128.     21: 'V',
  129.     30: '6',
  130.     4: 'E',
  131.     13: 'N',
  132.     22: 'W',
  133.     31: '7',
  134.     5: 'F',
  135.     14: 'O',
  136.     23: 'X',
  137.     6: 'G',
  138.     15: 'P',
  139.     24: 'Y',
  140.     7: 'H',
  141.     16: 'Q',
  142.     25: 'Z',
  143.     8: 'I',
  144.     17: 'R',
  145.     26: '2' }
  146. _b32tab = _b32alphabet.items()
  147. _b32tab.sort()
  148. _b32tab = [ v for k, v in _b32tab ]
  149. _b32rev = dict([ (v, long(k)) for k, v in _b32alphabet.items() ])
  150.  
  151. def b32encode(s):
  152.     '''Encode a string using Base32.
  153.  
  154.     s is the string to encode.  The encoded string is returned.
  155.     '''
  156.     parts = []
  157.     (quanta, leftover) = divmod(len(s), 5)
  158.     if leftover:
  159.         s += '\x00' * (5 - leftover)
  160.         quanta += 1
  161.     for i in range(quanta):
  162.         (c1, c2, c3) = struct.unpack('!HHB', s[i * 5:(i + 1) * 5])
  163.         c2 += (c1 & 1) << 16
  164.         c3 += (c2 & 3) << 8
  165.         parts.extend([
  166.             _b32tab[c1 >> 11],
  167.             _b32tab[c1 >> 6 & 31],
  168.             _b32tab[c1 >> 1 & 31],
  169.             _b32tab[c2 >> 12],
  170.             _b32tab[c2 >> 7 & 31],
  171.             _b32tab[c2 >> 2 & 31],
  172.             _b32tab[c3 >> 5],
  173.             _b32tab[c3 & 31]])
  174.     
  175.     encoded = EMPTYSTRING.join(parts)
  176.     if leftover == 1:
  177.         return encoded[:-6] + '======'
  178.     if None == 2:
  179.         return encoded[:-4] + '===='
  180.     if None == 3:
  181.         return encoded[:-3] + '==='
  182.     if None == 4:
  183.         return encoded[:-1] + '='
  184.  
  185.  
  186. def b32decode(s, casefold = False, map01 = None):
  187.     '''Decode a Base32 encoded string.
  188.  
  189.     s is the string to decode.  Optional casefold is a flag specifying whether
  190.     a lowercase alphabet is acceptable as input.  For security purposes, the
  191.     default is False.
  192.  
  193.     RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
  194.     (oh), and for optional mapping of the digit 1 (one) to either the letter I
  195.     (eye) or letter L (el).  The optional argument map01 when not None,
  196.     specifies which letter the digit 1 should be mapped to (when map01 is not
  197.     None, the digit 0 is always mapped to the letter O).  For security
  198.     purposes the default is None, so that 0 and 1 are not allowed in the
  199.     input.
  200.  
  201.     The decoded string is returned.  A TypeError is raised if s were
  202.     incorrectly padded or if there are non-alphabet characters present in the
  203.     string.
  204.     '''
  205.     (quanta, leftover) = divmod(len(s), 8)
  206.     if leftover:
  207.         raise TypeError('Incorrect padding')
  208.     if map01:
  209.         s = _translate(s, {
  210.             '0': 'O',
  211.             '1': map01 })
  212.     if casefold:
  213.         s = s.upper()
  214.     padchars = 0
  215.     mo = re.search('(?P<pad>[=]*)$', s)
  216.     if mo:
  217.         padchars = len(mo.group('pad'))
  218.         if padchars > 0:
  219.             s = s[:-padchars]
  220.         
  221.     parts = []
  222.     acc = 0
  223.     shift = 35
  224.     for c in s:
  225.         val = _b32rev.get(c)
  226.         if val is None:
  227.             raise TypeError('Non-base32 digit found')
  228.         acc += _b32rev[c] << shift
  229.         shift -= 5
  230.         if shift < 0:
  231.             parts.append(binascii.unhexlify('%010x' % acc))
  232.             acc = 0
  233.             shift = 35
  234.             continue
  235.     last = binascii.unhexlify('%010x' % acc)
  236.     if padchars == 0:
  237.         last = ''
  238.     elif padchars == 1:
  239.         last = last[:-1]
  240.     elif padchars == 3:
  241.         last = last[:-2]
  242.     elif padchars == 4:
  243.         last = last[:-3]
  244.     elif padchars == 6:
  245.         last = last[:-4]
  246.     else:
  247.         raise TypeError('Incorrect padding')
  248.     None.append(last)
  249.     return EMPTYSTRING.join(parts)
  250.  
  251.  
  252. def b16encode(s):
  253.     '''Encode a string using Base16.
  254.  
  255.     s is the string to encode.  The encoded string is returned.
  256.     '''
  257.     return binascii.hexlify(s).upper()
  258.  
  259.  
  260. def b16decode(s, casefold = False):
  261.     '''Decode a Base16 encoded string.
  262.  
  263.     s is the string to decode.  Optional casefold is a flag specifying whether
  264.     a lowercase alphabet is acceptable as input.  For security purposes, the
  265.     default is False.
  266.  
  267.     The decoded string is returned.  A TypeError is raised if s were
  268.     incorrectly padded or if there are non-alphabet characters present in the
  269.     string.
  270.     '''
  271.     if casefold:
  272.         s = s.upper()
  273.     if re.search('[^0-9A-F]', s):
  274.         raise TypeError('Non-base16 digit found')
  275.     return binascii.unhexlify(s)
  276.  
  277. MAXLINESIZE = 76
  278. MAXBINSIZE = (MAXLINESIZE // 4) * 3
  279.  
  280. def encode(input, output):
  281.     '''Encode a file.'''
  282.     while True:
  283.         s = input.read(MAXBINSIZE)
  284.         if not s:
  285.             break
  286.         while len(s) < MAXBINSIZE:
  287.             ns = input.read(MAXBINSIZE - len(s))
  288.             if not ns:
  289.                 break
  290.             s += ns
  291.         line = binascii.b2a_base64(s)
  292.         output.write(line)
  293.  
  294.  
  295. def decode(input, output):
  296.     '''Decode a file.'''
  297.     while True:
  298.         line = input.readline()
  299.         if not line:
  300.             break
  301.         s = binascii.a2b_base64(line)
  302.         output.write(s)
  303.  
  304.  
  305. def encodestring(s):
  306.     '''Encode a string into multiple lines of base-64 data.'''
  307.     pieces = []
  308.     for i in range(0, len(s), MAXBINSIZE):
  309.         chunk = s[i:i + MAXBINSIZE]
  310.         pieces.append(binascii.b2a_base64(chunk))
  311.     
  312.     return ''.join(pieces)
  313.  
  314.  
  315. def decodestring(s):
  316.     '''Decode a string.'''
  317.     return binascii.a2b_base64(s)
  318.  
  319.  
  320. def test():
  321.     '''Small test program'''
  322.     import sys as sys
  323.     import getopt as getopt
  324.     
  325.     try:
  326.         (opts, args) = getopt.getopt(sys.argv[1:], 'deut')
  327.     except getopt.error:
  328.         msg = None
  329.         sys.stdout = sys.stderr
  330.         print msg
  331.         print "usage: %s [-d|-e|-u|-t] [file|-]\n        -d, -u: decode\n        -e: encode (default)\n        -t: encode and decode string 'Aladdin:open sesame'" % sys.argv[0]
  332.         sys.exit(2)
  333.  
  334.     func = encode
  335.     for o, a in opts:
  336.         if o == '-e':
  337.             func = encode
  338.         if o == '-d':
  339.             func = decode
  340.         if o == '-u':
  341.             func = decode
  342.         if o == '-t':
  343.             test1()
  344.             return None
  345.     
  346.     if args and args[0] != '-':
  347.         with open(args[0], 'rb') as f:
  348.             func(f, sys.stdout)
  349.     else:
  350.         func(sys.stdin, sys.stdout)
  351.  
  352.  
  353. def test1():
  354.     s0 = 'Aladdin:open sesame'
  355.     s1 = encodestring(s0)
  356.     s2 = decodestring(s1)
  357.     print s0, repr(s1), s2
  358.  
  359. if __name__ == '__main__':
  360.     test()
  361.